$(function() {
    var grid = $("#gridUsers").datagrid({
        title: "用户信息",
        iconCls: "icon-save",
        methord: "post",
        url: "/System/GetUsersList/",
        //sortName: "ID",
        //sortOrder: "desc",
        idField: "ID",
        //pageSize: 20,
        striped: true, //奇偶行是否区分
        frozenColumns: [[
	                { field: "ck", checkbox: true }
				]],
        columns: [[
                    { field: "UserName", title: "登陆用户名"},
					{ field: "TrueName", title: "真实姓名"},
					{ field: "OfficeID", title: "所属部（室）", formatter: function(value, row, index) {
							var name = "";
							$.ajax({
								url: "/main/GetOfficeName?id=" + value,
								type: "post",
								async: false,
								success: function(re){
									var data = eval("(" + re + ")");
									name=data.OfficeName;
								}
							});
							return name;
						}},
					{ field: "AuthID", title: "用户级别"},
					{ field: "Activate", title: "是否可用",formatter: function(value, row, index) {
							if(value){
								return "是";
							}
							else{
								return "否";
							}
					}}
				]],
        fit: true,
        //pagination: true,
        rownumbers: true,
        fitColumns: true,
        singleSelect: false,
		onLoadError: function(data, status, error){
			if(data.status == "400")
				indexPage.reLogin();
		}
    });
    $("body").layout();
});

var UsersManage = {};
UsersManage.addForm = $("#addUsersForm");
UsersManage.DataGridAdd = function(){
	 $("#dlgUsers").dialog({
		title: "添加用户信息",
		modal: true,
		resizable: true,
		width: 900,
		height: 405,
		toolbar: [{
					text: "保存",
					iconCls: "icon-save",
					handler: function(){
						UsersManage.UsersAddFormSaveData();
					}
				}, "-", {
					text: "关闭",
					iconCls: "icon-no",
					handler: function () {
						$("#dlgUsers").dialog("close");
					}
				}],
		href: "/System/AddUser",
		onLoad: function(){
				    $("#addUsersForm").form("clear");
			},
		onLoadError: function(data, status, error){
			if(data.status == "400")
				indexPage.reLogin();
		}
	});
}

UsersManage.DataGridEdit = function(){
	var rows = $("#gridUsers").datagrid("getSelections");
    var num = rows.length;
    if (num == 0) {
        $.messager.alert("提示:","请选择一条记录进行操作!","info");
        return;
    }
    else if (num > 1) {
        $.messager.alert("提示","只能选择一条记录进行操作!","info"); 
        return;
    }
    else {
		$("#dlgUsers").dialog({
			title: "修改客户信息",
			modal: true,
			resizable: true,
			width: 900,
			height: 405,
			toolbar: [{
						text: "保存",
						iconCls: "icon-save",
						handler: function(){
							UsersManage.UsersEditFormSaveData();
						}
					}, "-", {
						text: "关闭",
						iconCls: "icon-no",
						handler: function () {
							$("#dlgUsers").dialog("close");
						}
					}],
			href: "/System/AddUser",
			onLoad: function(){
				    $("#addUsersForm").form("clear").form("load", rows[0]);
			},
			onLoadError: function(data, status, error){
				if(data.status == "400")
					indexPage.reLogin();
			}
		});
			
		}
}

UsersManage.DataGridDel = function() {
    var ids = [];
    var rows = $("#gridUsers").datagrid("getSelections");
    for (var i = 0; i < rows.length; i++) {
		ids.push(rows[i].ID);
    }
    if (ids.length > 0) {
        $.messager.confirm("提示", "您确认要删除选中的用户吗?", function (data) {
            if (data) {
                $.ajax({
                    url: "/System/DeleteUsers?ids=" + ids.join(","),
                    type: "post",
                    success: function (re) {
                        var data = eval("(" + re + ")");
                        if (data.success) {
                            $.messager.alert("提示",ids.length + "条记录" + data.msg,"info"); //提示消息
                            $("#gridUsers").datagrid("reload");
                            $("#gridUsers").datagrid("clearSelections"); //清除所有选中的元素
                        } else {
                            $.messager.alert("错误", data.msg, "error");
                        }
                    },
					error: function(data, status, error){
						if(data.status == "400")
							indexPage.reLogin();
						else{
							$.messager.alert("错误", "删除失败!", "error");
                            $("#grid").datagrid("clearSelections");
						}
					}
                });
            }
        });
    } else {
        $.messager.alert("提示","请先选择要删除的用户。","info");

    }
}

UsersManage.DataGridShutDown = function() {
    var ids = [];
    var rows = $("#gridUsers").datagrid("getSelections");
    for (var i = 0; i < rows.length; i++) {
		ids.push(rows[i].ID);
    }
    if (ids.length > 0) {
        $.messager.confirm("提示", "您确认要停用选中的用户吗?", function (data) {
            if (data) {
                $.ajax({
                    url: "/System/SetUserDown?ids=" + ids.join(","),
                    type: "post",
                    error: function () {
                        $.messager.alert("错误", "停用失败!", "error");
                        $("#gridUsers").datagrid("clearSelections");
                    },
                    success: function (re) {
                        var data = eval("(" + re + ")");
                        if (data.success) {
                            $.messager.alert("提示",ids.length + "条记录" + data.msg,"info"); //提示消息
                            $("#gridUsers").datagrid("reload");
                            $("#gridUsers").datagrid("clearSelections"); //清除所有选中的元素
                        } else {
                            $.messager.alert("错误", data.msg, "error");
                        }
                    },
					error: function (data, status, error) {      // 设置表单提交出错                 
						if(data.status == "400")
							indexPage.reLogin();
					}
                });
            }
        });
    } else {
        $.messager.alert("提示","请先选择要停用的用户。","info");

    }
}

UsersManage.UsersAddFormSaveData = function(){
	// $("#addUsersForm").form("submit", {
        // url: "System/SaveUsers",
        // onSubmit: function (param) {
			// var ids = [];
			// var rows = $("#userAuthTree").datagrid("getSelections");
			// for (var i = 0; i < rows.length; i++) {
				// ids.push(rows[i].id);
			// }
			// param.ids = ids.join(",");
            // return $(this).form("validate");
        // },
        // success: function(data){
			// var data = eval("(" + data + ")");
			// if (data.success){
				// $.messager.alert("提示",data.msg,"info");
				// $("#addUsersForm").form("clear");
				// $("#gridUsers").datagrid("reload");
			// }
			// else{
				// $.messager.alert("错误",data.msg,"error");
			// }
		// }
    // });
	if($("#addUsersForm").form('validate'))
	{
		var ids = [];
		var id="";
		var rows = $("#userAuthTree").datagrid("getSelections");
		for (var i = 0; i < rows.length; i++) {
			ids.push(rows[i].id);
		}
		id = ids.join(",");
		$.ajax({
            type: "POST",   //post提交方式默认是get
            url: "System/SaveUsers",
            data: $("#addUsersForm").serialize()+"&ids="+id,   //序列化               
            error: function (data, status, error) {      // 设置表单提交出错                 
                if(data.status == "400")
				    indexPage.reLogin();
            },
            success: function (data) {
               var data = eval("(" + data + ")");
			    if (data.success){
				    $.messager.alert("提示",data.msg,"info");
				    $("#addUsersForm").form("clear");
				    $("#gridUsers").datagrid("reload");
			    }
			    else{
				    $.messager.alert("错误",data.msg,"error");
			    }
            }
        })
	}
}

UsersManage.UsersEditFormSaveData = function(){
	// $("#addUsersForm").form("submit", {
        // url: "System/EditUsers",
        // onSubmit: function (param) {
			// var ids = [];
			// var rows = $("#userAuthTree").datagrid("getSelections");
			// for (var i = 0; i < rows.length; i++) {
				// ids.push(rows[i].id);
			// }
			// param.ids = ids.join(",");
            // return $(this).form("validate");
        // },
        // success: function(data){
			// var data = eval("(" + data + ")");
			// if (data.success){
				// $("#addUsersForm").form("clear");
				// $("#gridUsers").datagrid("reload");
				// $("#dlgUsers").dialog("close");
				// $.messager.alert("提示",data.msg,"info");
			// }
			// else{
				// $.messager.alert("错误",data.msg,"error");
			// }
		// }
    // });
	if($("#addUsersForm").form('validate'))
	{
		var ids = [];
		var id="";
		var rows = $("#userAuthTree").datagrid("getSelections");
		for (var i = 0; i < rows.length; i++) {
			ids.push(rows[i].id);
		}
		id = ids.join(",");
		$.ajax({
            type: "POST",   //post提交方式默认是get
            url: "System/EditUsers",
            data: $("#addUsersForm").serialize()+"&ids="+id,   //序列化               
            error: function (data, status, error) {      // 设置表单提交出错                 
                if(data.status == "400")
				    indexPage.reLogin();
            },
            success: function (data) {
               var data = eval("(" + data + ")");
			    if (data.success){
				    $.messager.alert("提示",data.msg,"info");
					$("#addUsersForm").form("clear");
					$("#gridUsers").datagrid("reload");
					$("#dlgUsers").dialog("close");
			    }
			    else{
				    $.messager.alert("错误",data.msg,"error");
			    }
            }
        })
	}
}